/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataClient.java
 *
 * Created on 04-12-2011, 09:44:52 PM
 */
package socket.dataserver.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java2jdbc.model.Cliente;
import java2jdbc.model.Producto;

/**
 *
 * @author gabrielbran
 */
public class DataClient extends javax.swing.JFrame {

    /** Creates new form DataClient */
    public DataClient() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txMensaje = new javax.swing.JTextArea();
        optCliente = new javax.swing.JRadioButton();
        optProducto = new javax.swing.JRadioButton();
        btCargar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txMensaje.setColumns(20);
        txMensaje.setRows(5);
        jScrollPane1.setViewportView(txMensaje);

        optCliente.setSelected(true);
        optCliente.setText("Cliente");

        optProducto.setText("Producto");

        btCargar.setText("Cargar");
        btCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 729, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addComponent(btCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(optCliente)
                        .addGap(33, 33, 33)
                        .addComponent(optProducto)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(optProducto)
                    .addComponent(btCargar)
                    .addComponent(optCliente))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCargarActionPerformed
        try {
            Socket clienteSocket = new Socket("127.0.0.1", 1500);
            OutputStream os = clienteSocket.getOutputStream();
            PrintWriter pw = new PrintWriter(os, true);

            if (optCliente.isSelected()) {
                pw.println("cliente");
                txMensaje.append("Obteniendo lista de Clientes\n");
            } else if (optProducto.isSelected()) {
                pw.println("producto");
                txMensaje.append("Obteniendo lista de Productos\n");
            }
            List<Object> elementos = new ArrayList();
            ObjectInputStream ois = new ObjectInputStream(clienteSocket.getInputStream());
            try {

                elementos = (ArrayList) ois.readObject();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DataClient.class.getName()).log(Level.SEVERE, null, ex);
            }
            ois.close();

            for (Object o : elementos) {
                if (o instanceof Cliente) {
                    Cliente c = (Cliente) o;
                    txMensaje.append("Cliente :" + c.toString() + "\n");
                } else if (o instanceof Producto) {
                    Producto p = (Producto) o;
                    txMensaje.append("Producto :" + p.toString() + "\n");
                }
            }
        } catch (UnknownHostException ex) {
            Logger.getLogger(DataClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btCargarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DataClient().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCargar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton optCliente;
    private javax.swing.JRadioButton optProducto;
    private javax.swing.JTextArea txMensaje;
    // End of variables declaration//GEN-END:variables
}
